/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package estates.validation;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

public class StringLengthValidator implements Validator{

        private int maxLength;

	public StringLengthValidator(){
            maxLength = 500;
	}

        public StringLengthValidator(int maxLength){
            this.maxLength = maxLength;
	}

	@Override
	public void validate(FacesContext context, UIComponent component,
			Object value) throws ValidatorException {

                if(value==null || value.toString().trim().length()==0)return;

		if(value.toString().length() > maxLength){

			FacesMessage msg =
				new FacesMessage("Field too long",
						"Field too long");
			msg.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(msg);

		}

	}
}
